package EvernoteOne;

import java.io.*;
import java.util.*;

public class Solution {
	public static void main(String[] args) throws Exception {
		//Create the buffer and read in a line.
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String line = br.readLine().trim();
        
        //Create an array of strings to represent the buffer.
        String buffer[] = new String[Integer.parseInt(line)];
        
        //Init the buffer.
        clearBuffer(buffer);
        
        //Create a variable to hold the current place in the buffer. (Will either be empty or the oldest entry.)
        int cursor = 0;
        
        while(true)
        {
        	line = br.readLine().trim();
        	
        	if(line.length()>0){
        		char command = line.charAt(0);
	        	if(command == 'A' || command == 'a')
	        	{
	        		//Get the number of lines to read in.
	        		int lines = Integer.parseInt(line.substring(1, line.length()).trim());
	        		
	        		//Read in items and append them to the buffer.
	        		cursor = readAndAppendToBuffer(buffer, cursor, lines, br);
	        	}
	        	else if(command == 'R' || command == 'r')
	        	{
	        		//Get the number of items to delete.
	        		int num = Integer.parseInt(line.substring(1, line.length()).trim());
	        		
	        		//Remove the oldest num entries.
	        		removeFromBuffer(buffer, cursor,  num);
	        	}
	        	else if(command == 'L' || command == 'l')
	        	{
	        		listBuffer(buffer, cursor);
	        	}
	        	else if(command == 'Q' || command == 'q')
	        	{
	        		break;
	        	}
        	}
        }
	}
	
	//Remove the oldest num entries.
	static void removeFromBuffer(String buf[], int cur, int num) 
	{
		if(num >= (buf.length-1))
		{
			clearBuffer(buf);
			cur = 0;
		}
		else
		{	
			//Step until you reach the oldest value.
			for(int i = 0; i < buf.length; i++){
				
				if(!buf[cur].equals(""))
				{
					break;
				}
				else
				{
					//If there are no values in the buffer, nothing needs to be done.
					if(i == buf.length-1)
					{
						return;
					}
					
					cur = incrementCursor(cur, buf.length-1);
				}
			}
			
			int count = 0;
			while(count < num)
			{
				buf[cur]="";
				cur = incrementCursor(cur, buf.length-1);
				count++;
			}
		}
	}
	
	//Read in the number of lines and append them to the buffer as appropriate.
	static int  readAndAppendToBuffer(String buf[], int cur, int numLines, BufferedReader br) throws Exception
	{	
		//Read in each line and append to the buffer.
		for(int i = 0; i < numLines; i++)
		{
			//Read in and append string.
			String in = br.readLine();
			buf[cur] = in;
			
			//Increment the cursor.
			cur = incrementCursor(cur, buf.length-1);
		}
		
		return cur;
	}
	
	//List the buffer.
	static void listBuffer(String buf[], int cursor)
	{	
		//Step until you reach the oldest value.
		for(int i = 0; i < buf.length; i++){
			String entry = buf[cursor];
			
			if(!entry.equals(""))
			{
				System.out.println(entry);
			}
			
			cursor = incrementCursor(cursor, buf.length-1);
		}
	}
	
	//Increment the cursor.
	static int incrementCursor(int cursor, int max)
	{
		if(cursor<max)
		{
			cursor = cursor + 1;
		}
		else
		{
			cursor = 0;
		}
		
		return cursor;
	}
	
	//Create default empty string values for each element in the buffer.
	static void clearBuffer(String buf[])
	{
		for(int i = 0; i < buf.length; i++)
		{
			buf[i] = "";
		}
	}
}
